<template>
  <div class="assets">
    <div class="assets_main">
      <van-list
        v-if="result.length"
        v-model="loading"
        :finished="finished"
        offset="30"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div
          class="item"
          v-for="(item, index) in result"
          :key="index"
          @click="productDetail(item.id)"
        >
          <div class="item_wrap">
            <div class="item_wrap_img">
              <img :src="`${item.image}`" alt="" style="width:100%;height:100%;" />
            </div>
            <div class="item_wrap_detail">
              <div class="item_wrap_title">{{ item.className }}</div>
              <div class="dep_name" v-if="item.depName">{{ item.depName }}</div>
              <div class="detail_title" v-if="item.remarks?.length">{{item.remarks}}</div>
            </div>
          </div>
        </div>
      </van-list>
      <div class="nodata" v-else>
        <van-empty description="暂无数据哦" />
      </div>
    </div>
  </div>
</template>

<script>
import { Empty, List } from 'vant';
const courseType = {
  excellentCourse: '2', // 精品课程
  publicCourse: '0', // 常规课程
  auditionCourse: '1', // 试听课程
}
export default {
  components: {
    [Empty.name]: Empty,
    [List.name]: List,
  },
  data() {
    return {
      source: courseType.auditionCourse,
      scrollBottom: false, // 是否通过滚动到底触发请求
      current: 1,
      size: 10,
      loading: false,
      finished: true,
      result: [],
    }
  },
  created() {
    this.source = this.$route.query.source ? courseType[this.$route.query.source] : courseType.auditionCourse;
    console.log(this.source, 'source')
    if (this.source === courseType.excellentCourse) {
      document.title = '精品课程列表'
    } else if(this.source === courseType.publicCourse) {
      document.title = '常规课程列表'
    }
    this.getOpenId()

    this.getData()
  },
  methods: {
    async getOpenId() {
      if (!window.localStorage.getItem('px_user_openid')) {
        let code = this.getCode(to.path)
        if (!code) return
        let ret = await this.$http.getUserIfRegister({ code })
        localStorage.setItem('px_user_openid', ret.data.result.openId)
        // 保存完整的登录数据到缓存
        this.$store.dispatch('saveLoginData', ret.data)
      }
    },
    onLoad() {
      this.current = this.current + 1
      this.scrollBottom = true
      this.getData()
    },
    getData() {
      let params = {
        current: this.current,
        size: this.size,
        typeId: this.source
      }
      this.$http.getAuditionList(params).then((res) => {
        let { records, current, pages } = res.data.result
          console.log(res)
          this.result = this.scrollBottom
            ? [...this.result].concat(records)
            : records
          this.scrollBottom = false
          if (current >= pages) this.finished = true
          else this.finished = false
        })
        .catch((err) => {
          console.log(err, 'err')
          this.$toast(err.data.message)
        })
    },
    productDetail(id) {
      console.log(id)
      this.$router.push({ name: 'common-product-detail', params: { id, from: 'orderCourseList' } })
    },
  },
}
</script>

<style lang="scss" scoped>
.assets {
  color: #333;
  height: 100%;
  .assets_main {
    height: calc(100% - 44px);
    width: 100%;
    overflow: hidden;
    .item:first-child {
      margin-top: 15px;
    }
    .item {
      width: calc(100% - 30px);
      margin-left: 15px;
      position: relative;
      margin-top: 10px;
      padding: 14px 16px;
      background: #fff;
      box-sizing: border-box;
      box-shadow: 0 4px 15px 0 rgba(0, 0, 0, 0.09);
      border-radius: 4px;
      display: flex;
      flex-direction: column;
      .item_wrap {
        display: flex;
        align-items: center;
        .item_wrap_img {
          width: 70px;
          height: 70px;
        }
        .item_wrap_detail {
          flex: 1;
          overflow: hidden;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          height: 100%;
          padding: 0px 10px;
          box-sizing: border-box;
          .item_wrap_title {
            width: 100%;
            min-height: 25px;
            font-weight: 900;
            text-align: left;
            color: #333;
          }
          .dep_name {
            font-size: 13px;
            color: #1890ff;
            margin: 4px 0;
          }
          .detail_title {
            width: 100%;
            font-size: 14px;
            color: #999;
            overflow:hidden;
            text-overflow:ellipsis;
            display:-webkit-box;
            -webkit-box-orient:vertical;
            -webkit-line-clamp:2;
          }
        }
      }
    }
    .nodata {
      width: 100%;
      height: 100%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
</style>
<style lang="scss">
.assets {
  .van-list {
    height: 100%;
    overflow: scroll;
  }
  .van-tab {
    font-size: 16px;
  }
}
</style>
